package com.lagou.demo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lagou.demo.api.CodeServiceApi;
import com.lagou.demo.pojo.User;
import com.lagou.demo.service.IUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

//@RefreshScope
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Reference
    private CodeServiceApi codeService;


//    @Value("${test.testStr}")
//    private String testStr;
//
//
    @GetMapping("test")
    public String test() {
        return "testStr";
    }


    @PostMapping("/register/{email}/{passwd}/{code}")
    public boolean register(@PathVariable("email") String email,
                            @PathVariable("passwd") String passwd,
                            @PathVariable("code") String code) {
        try {
            Integer validate = codeService.validate(email, code);
            if (validate != 0 ) {
                return false;
            }
            userService.insertUser(email, passwd);
            return true ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable("email") String email) {
        User user = new User();
        user.setEmail(email);
        try {
            User one = userService.findOne(user);
            if (one != null) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false ;
    }

    @PostMapping("/login/{email}/{passwd}")
    @SentinelResource(value = "login",
            blockHandlerClass = SentinelFallbackClass.class, blockHandler = "handleException",
            fallbackClass = SentinelFallbackClass.class, fallback = "handleError")
    public String login(@PathVariable("email") String email,
                        @PathVariable("passwd") String passwd,
                        HttpServletResponse response) {
        User user = new User();
        user.setEmail(email);
        user.setPasswd(passwd);
        try {
            User one = userService.findOne(user);
            if (one != null) {
                String token = UUID.randomUUID().toString().replaceAll("-", "");
                one.setToken(token);
                userService.save(one);
                Cookie cookie = new Cookie("token", token);
                cookie.setPath("/");
                cookie.setHttpOnly(true);
                cookie.setMaxAge(1000);
                cookie.setSecure(false);

                response.addCookie(cookie);
                return one.getEmail();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null ;
    }

    @PostMapping("/info/{token}")
    public String info(@PathVariable("token") String token) {
        String email = null;
        User user = new User();
        user.setToken(token);
        try {
            User one = userService.findOne(user);
            if (one != null) {
                email = one.getEmail();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return email;
    }


}
